#include "pch.h"
#include "capture.h"
#include "HisFX3Platform.h"
#include "SystemLog.h"
#include "SystemManager.h"
#include "XIStream.h"
#include"XIProperty.h"
#include"DeviceDefs.h"
#include"XIProperty.h"
#include<atlconv.h>
// 

extern CSystemManager    gSystemMgr;
extern CSystemLog     gSystemLog;


#pragma comment(lib, "LibXIStream2.lib")
#pragma comment(lib, "LibXIProperty.lib")
#define CAPTURE_COLORSPACE		XI_COLOR_YUYV // XI_COLOR_NV12
#define FRAME_DURATION_25		400000

#define CAPTURE_WIDTH			640  /*720*/
#define CAPTURE_HEIGHT			480

HANDLE hproperty = NULL;
HANDLE hvideo_device = NULL;
HANDLE hrender = NULL;

GUID   guid_monitor;
int	   frame_rate = FRAME_DURATION_25;
BOOL   use_custom = FALSE;
int		video_device_select = -1;

CString cstrLog = L"";
BYTE	RtblITU[256][256];
BYTE	GtblITU[256][256][256];
BYTE	BtblITU[256][256];



void	MakeSpaceTableITU(void)
{
	int		Y, Cr, Cb, tmp;
	for (Y = 0; Y < 256; Y++) {	//Y
		for (Cr = 0; Cr < 256; Cr++) { //Cr
			for (Cb = 0; Cb < 256; Cb++) { //Cb
				//B = 1.164(Y-16)+2.018(Cr-128)
				tmp = (int)(1.164*(Y - 16) + 2.018*(Cb - 128));
				if (tmp > 255) tmp = 255;
				if (tmp < 0) tmp = 0;
				BtblITU[Y][Cb] = tmp;
				tmp = (int)(1.164*(Y - 16) + 1.596*(Cr - 128));
				if (tmp > 255) tmp = 255;
				if (tmp < 0) tmp = 0;
				RtblITU[Y][Cr] = tmp;
			}
		}
	}
}

void YCbCrITUToBmp(int iMode, BYTE *pYuvBuffer, BYTE *pBmpBuffer, int size_x, int size_y)
{
	DWORD		 Y0, Y1, Cb, Cr; //speed up!!!
	int			 tmp;
	int			 i, j;
	LPBYTE       pImageData, pImageTemp;

	pImageData = pYuvBuffer + size_x * 2 * (size_y - 1);

	switch (iMode) {
	case 3: //CbYCrY
		for (i = 0; i < size_y; i++) {
			pImageTemp = pImageData;
			for (j = 0; j < size_x; j += 2) {
				Y0 = pImageTemp[1];
				Cr = pImageTemp[2];
				Y1 = pImageTemp[3];
				Cb = pImageTemp[0];
				*pBmpBuffer++ = BtblITU[Y0][Cb];    //B
				tmp = (int)(1192 * (Y0 - 16) - 832 * (Cr - 128) - 400 * (Cb - 128)) >> 10;
				if (tmp > 255)  tmp = 255;
				if (tmp < 0)    tmp = 0;
				*pBmpBuffer++ = tmp;				//G
				*pBmpBuffer++ = RtblITU[Y0][Cr];	//R

				*pBmpBuffer++ = BtblITU[Y1][Cb];	 //B
				tmp = (int)(1192 * (Y1 - 16) - 832 * (Cr - 128) - 400 * (Cb - 128)) >> 10;
				if (tmp > 255)  tmp = 255;
				if (tmp < 0)    tmp = 0;
				*pBmpBuffer++ = tmp;				 //G
				*pBmpBuffer++ = RtblITU[Y1][Cr];	 //R
				pImageTemp += 4;  //next pair.
			}
			pImageData -= size_x * 2;
		}
		break;
	case 2: //YCrYCb
		for (i = 0; i < size_y; i++) {
			pImageTemp = pImageData;
			for (j = 0; j < size_x; j += 2) {
				Y0 = pImageTemp[0];
				Cr = pImageTemp[1];
				Y1 = pImageTemp[2];
				Cb = pImageTemp[3];
				*pBmpBuffer++ = BtblITU[Y0][Cb];		 //B
				tmp = (int)(1192 * (Y0 - 16) - 832 * (Cr - 128) - 400 * (Cb - 128)) >> 10;
				if (tmp > 255)  tmp = 255;
				if (tmp < 0)    tmp = 0;
				*pBmpBuffer++ = tmp;				 //G
				*pBmpBuffer++ = RtblITU[Y0][Cr];     //R

				*pBmpBuffer++ = BtblITU[Y1][Cb];	 //B	
				tmp = (int)(1192 * (Y1 - 16) - 832 * (Cr - 128) - 400 * (Cb - 128)) >> 10;
				if (tmp > 255)  tmp = 255;
				if (tmp < 0)    tmp = 0;
				*pBmpBuffer++ = tmp;				 //G
				*pBmpBuffer++ = RtblITU[Y1][Cr];     //R
				pImageTemp += 4;	//next  pair.
			}
			pImageData -= size_x * 2;
		}
		break;
	case 1: //YCbYCr
		for (i = 0; i < size_y; i++) {
			pImageTemp = pImageData;
			for (j = 0; j < size_x; j += 2) {
				Y0 = pImageTemp[0];
				Cr = pImageTemp[3];
				Y1 = pImageTemp[2];
				Cb = pImageTemp[1];
				*pBmpBuffer++ = BtblITU[Y0][Cb];		 //B
				tmp = (int)(1192 * (Y0 - 16) - 832 * (Cr - 128) - 400 * (Cb - 128)) >> 10;
				if (tmp > 255)  tmp = 255;
				if (tmp < 0)    tmp = 0;
				*pBmpBuffer++ = tmp;				 //G
				*pBmpBuffer++ = RtblITU[Y0][Cr];	 //R

				*pBmpBuffer++ = BtblITU[Y1][Cb];	 //B
				tmp = (int)(1192 * (Y1 - 16) - 832 * (Cr - 128) - 400 * (Cb - 128)) >> 10;
				if (tmp > 255)  tmp = 255;
				if (tmp < 0)    tmp = 0;
				*pBmpBuffer++ = tmp;				 //G
				*pBmpBuffer++ = RtblITU[Y1][Cr];	 //R
				pImageTemp += 4;  //next YCbYCr pair.
			}
			pImageData -= size_x * 2;
		}
		break;
	case 4: //CrYCbY
		for (i = 0; i < size_y; i++) {
			pImageTemp = pImageData;
			for (j = 0; j < size_x; j += 2) {
				Y0 = pImageTemp[1];
				Cr = pImageTemp[0];
				Y1 = pImageTemp[3];
				Cb = pImageTemp[2];
				*pBmpBuffer++ = BtblITU[Y0][Cb];     //B
				tmp = (int)(1192 * (Y0 - 16) - 832 * (Cr - 128) - 400 * (Cb - 128)) >> 10;
				if (tmp > 255)  tmp = 255;
				if (tmp < 0)    tmp = 0;
				*pBmpBuffer++ = tmp;				 //G
				*pBmpBuffer++ = RtblITU[Y0][Cr];	 //R

				*pBmpBuffer++ = BtblITU[Y1][Cb];   //B
				tmp = (int)(1192 * (Y1 - 16) - 832 * (Cr - 128) - 400 * (Cb - 128)) >> 10;
				if (tmp > 255)  tmp = 255;
				if (tmp < 0)    tmp = 0;
				*pBmpBuffer++ = tmp;				 //G
				*pBmpBuffer++ = RtblITU[Y1][Cr];	 //R
				pImageTemp += 4;  //next  pair
			}
			pImageData -= size_x * 2;
		}
		break;
	}
}


void VideoCaptureCallbackInternal(const BYTE * pbyImage, int cbImageStride, UINT64 u64TimeStamp)
{
	XIS_VideoRendererDrawImage(hrender, pbyImage, cbImageStride, TRUE);
	memcpy(gSystemMgr.imgCap->bImgBuf, pbyImage, gSystemMgr.imgCap->iImgWidth*gSystemMgr.imgCap->iImgHeight * 2);
	gSystemMgr.changeImg.YCbCrITUToBmp(1, gSystemMgr.imgCap->bImgBuf, gSystemMgr.imgCap->bBmpBuf, gSystemMgr.imgCap->iImgWidth, gSystemMgr.imgCap->iImgHeight);
	gSystemMgr.imgCap->iBmpW = gSystemMgr.imgCap->iImgWidth;
	gSystemMgr.imgCap->iBmpH = gSystemMgr.imgCap->iImgHeight;
	if (gSystemMgr.m_bMirrorX)
	{
		if (gSystemMgr.m_bMirrorY)
		{
			for (int y = 0; y < gSystemMgr.imgCap->iBmpH; y++)
			{
				for (int x = 0; x < gSystemMgr.imgCap->iBmpW; x++)
				{
					gSystemMgr.imgCap->bBmpTestBuf[y * 3 * gSystemMgr.imgCap->iBmpW + x * 3 + 0] = gSystemMgr.imgCap->bBmpBuf[y * 3 * gSystemMgr.imgCap->iBmpW + x * 3];
					gSystemMgr.imgCap->bBmpTestBuf[y * 3 * gSystemMgr.imgCap->iBmpW + x * 3 + 1] = gSystemMgr.imgCap->bBmpBuf[y * 3 * gSystemMgr.imgCap->iBmpW + x * 3 + 1];
					gSystemMgr.imgCap->bBmpTestBuf[y * 3 * gSystemMgr.imgCap->iBmpW + x * 3 + 2] = gSystemMgr.imgCap->bBmpBuf[y * 3 * gSystemMgr.imgCap->iBmpW + x * 3 + 2];
				}
			}
		}
		else
		{
			for (int y = 0; y < gSystemMgr.imgCap->iBmpH; y++)
			{
				for (int x = 0; x < gSystemMgr.imgCap->iBmpW; x++)
				{
					gSystemMgr.imgCap->bBmpTestBuf[y * 3 * gSystemMgr.imgCap->iBmpW + x * 3 + 0] = gSystemMgr.imgCap->bBmpBuf[(gSystemMgr.imgCap->iBmpH - 1 - y) * 3 * gSystemMgr.imgCap->iBmpW + x * 3];
					gSystemMgr.imgCap->bBmpTestBuf[y * 3 * gSystemMgr.imgCap->iBmpW + x * 3 + 1] = gSystemMgr.imgCap->bBmpBuf[(gSystemMgr.imgCap->iBmpH - 1 - y) * 3 * gSystemMgr.imgCap->iBmpW + x * 3 + 1];
					gSystemMgr.imgCap->bBmpTestBuf[y * 3 * gSystemMgr.imgCap->iBmpW + x * 3 + 2] = gSystemMgr.imgCap->bBmpBuf[(gSystemMgr.imgCap->iBmpH - 1 - y) * 3 * gSystemMgr.imgCap->iBmpW + x * 3 + 2];
				}
			}
		}
	}
	else
	{
		if (gSystemMgr.m_bMirrorY)
		{
			for (int y = 0; y < gSystemMgr.imgCap->iBmpH; y++)
			{
				for (int x = 0; x < gSystemMgr.imgCap->iBmpW; x++)
				{
					gSystemMgr.imgCap->bBmpTestBuf[y * 3 * gSystemMgr.imgCap->iBmpW + x * 3 + 0] = gSystemMgr.imgCap->bBmpBuf[y * 3 * gSystemMgr.imgCap->iBmpW + (gSystemMgr.imgCap->iBmpW - 1 - x) * 3];
					gSystemMgr.imgCap->bBmpTestBuf[y * 3 * gSystemMgr.imgCap->iBmpW + x * 3 + 1] = gSystemMgr.imgCap->bBmpBuf[y * 3 * gSystemMgr.imgCap->iBmpW + (gSystemMgr.imgCap->iBmpW - 1 - x) * 3 + 1];
					gSystemMgr.imgCap->bBmpTestBuf[y * 3 * gSystemMgr.imgCap->iBmpW + x * 3 + 2] = gSystemMgr.imgCap->bBmpBuf[y * 3 * gSystemMgr.imgCap->iBmpW + (gSystemMgr.imgCap->iBmpW - 1 - x) * 3 + 2];
				}
			}
		}
		else
		{
			for (int y = 0; y < gSystemMgr.imgCap->iBmpH; y++)
			{
				for (int x = 0; x < gSystemMgr.imgCap->iBmpW; x++)
				{
					gSystemMgr.imgCap->bBmpTestBuf[y * 3 * gSystemMgr.imgCap->iBmpW + x * 3 + 0] = gSystemMgr.imgCap->bBmpBuf[(gSystemMgr.imgCap->iBmpH - 1 - y) * 3 * gSystemMgr.imgCap->iBmpW + (gSystemMgr.imgCap->iBmpW - 1 - x) * 3];
					gSystemMgr.imgCap->bBmpTestBuf[y * 3 * gSystemMgr.imgCap->iBmpW + x * 3 + 1] = gSystemMgr.imgCap->bBmpBuf[(gSystemMgr.imgCap->iBmpH - 1 - y) * 3 * gSystemMgr.imgCap->iBmpW + (gSystemMgr.imgCap->iBmpW - 1 - x) * 3 + 1];
					gSystemMgr.imgCap->bBmpTestBuf[y * 3 * gSystemMgr.imgCap->iBmpW + x * 3 + 2] = gSystemMgr.imgCap->bBmpBuf[(gSystemMgr.imgCap->iBmpH - 1 - y) * 3 * gSystemMgr.imgCap->iBmpW + (gSystemMgr.imgCap->iBmpW - 1 - x) * 3 + 2];
				}
			}
		}
	}
}

void VideoCaptureCallback(const BYTE * pbyImage, int cbImageStride, void * pvParam, UINT64 u64TimeStamp) {

	VideoCaptureCallbackInternal(pbyImage, cbImageStride, u64TimeStamp);
}

BOOL resize_window(BOOL use_default)
{

	if (use_default && hproperty != NULL) {
		XI_DEVICE_TYPE devType = XIP_GetDeviceType(hproperty);
		int nLeft, nTop, nWidth = 0, nHeight = 0;

		if (XI_DEVICE_HDVIDEO_CAPTURE == devType || PRO_CAPTURE == devType) {
			XIPHD_GetDefClip(hproperty, &nLeft, &nTop, &nWidth, &nHeight);
		}
		else {
			XIPCVBS_GetClip(hproperty, &nLeft, &nTop, &nWidth, &nHeight);
		}

		cstrLog.Format(L"left=%d, top=%d, width=%d, height=%d", nLeft, nTop, nWidth, nHeight);
		gSystemLog.DisplayLogAndSave(cstrLog.GetBuffer(), LV_EVENT);

		// 		if (nWidth == 0 || nHeight == 0){
		// 			return FALSE;
		// 		}

		nWidth = max(CAPTURE_WIDTH, min(2048, nWidth));
		nHeight = max(CAPTURE_HEIGHT, min(1536, nHeight));

		gSystemMgr.imgCap->iImgWidth = nWidth;
		gSystemMgr.imgCap->iImgHeight = nHeight;
		cstrLog.Format(L"imgW = %d, imgH = %d", gSystemMgr.imgCap->iImgWidth, gSystemMgr.imgCap->iImgHeight);
		gSystemLog.DisplayLogAndSave(cstrLog.GetBuffer(), LV_EVENT);
		use_custom = FALSE;
	}

	XIS_StopVideoCapture(hvideo_device);

	if (!XIS_SetVideoCaptureFormat(hvideo_device, CAPTURE_COLORSPACE, gSystemMgr.imgCap->iImgWidth, gSystemMgr.imgCap->iImgHeight, frame_rate)) {
		gSystemLog.DisplayLogAndSave(L"set video format fail.", LV_ERROR);
		return FALSE;
	}
	else {
		XIS_SetVideoCaptureCallbackEx(hvideo_device, VideoCaptureCallback, NULL, FALSE);
	}

	if (!XIS_StartVideoCapture(hvideo_device)) {
		gSystemLog.DisplayLogAndSave(L"start video capture fail.", LV_ERROR);
		return FALSE;
	}

	return TRUE;
}

BOOL open_device_306e(int id)
{
	if (video_device_select == id) {
		return TRUE;
	}
	cstrLog.Format(L"open cap device=%d", id);
	gSystemLog.DisplayLogAndSave(cstrLog.GetBuffer(), LV_EVENT);
	if (video_device_select != -1) {
		if (hproperty) {
			XIP_ClosePropertyHandle(hproperty);
			hproperty = NULL;
		}

		if (hvideo_device) {
			XIS_StopVideoCapture(hvideo_device);
			XIS_CloseVideoCapture(hvideo_device);
			hvideo_device = NULL;
		}
		if (hrender) {
			XIS_DestroyVideoRenderer(hrender);
			hrender = NULL;
		}
	}

	VIDEO_CAPTURE_INFO vid_cap_info;
	if (!XIS_GetVideoCaptureInfo(id, &vid_cap_info)) {
		gSystemLog.DisplayLogAndSave(L"get video capture info fail.", LV_ERROR);
		return FALSE;
	}
	else {
		USES_CONVERSION;
		cstrLog.Format(L"id=%s", CString(vid_cap_info.szDShowID).GetString());
		gSystemLog.DisplayLogAndSave(cstrLog.GetBuffer(), LV_EVENT);
		cstrLog.Format(L"name=%s", CString(vid_cap_info.szName).GetString());
		gSystemLog.DisplayLogAndSave(cstrLog.GetBuffer(), LV_EVENT);
	}

	hvideo_device = XIS_OpenVideoCapture(vid_cap_info.szDShowID);
	if (NULL == hvideo_device) {
		gSystemLog.DisplayLogAndSave(L"open video capture fail.", LV_ERROR);
		return FALSE;
	}
	else {
		gSystemLog.DisplayLogAndSave(L"open video capture ok.", LV_EVENT);
	}

	XIS_SetVideoCaptureBuffering(hvideo_device, FALSE);
	hproperty = XIS_OpenVideoCapturePropertyHandle(hvideo_device);
	if (NULL != hproperty) {
		char serail_no[MAX_SERIAL_NO] = { 0 };
		XIP_GetSerialNo(hproperty, serail_no);
		cstrLog.Format(L"serial:%s",CString( serail_no).GetString());
		gSystemLog.DisplayLogAndSave(cstrLog.GetBuffer(), LV_EVENT);
	}
	else {
		gSystemLog.DisplayLogAndSave(L"get video property fail.", LV_ERROR);
	}

	if (!XIS_GetMonitorGUIDFromWindow(NULL, &guid_monitor)) {
		guid_monitor = GUID_NULL;	
	}
	cstrLog.Format(L"init w:%d, h:%d", gSystemMgr.IMG_W, gSystemMgr.IMG_H);
	gSystemMgr.imgCap->iImgHeight = gSystemMgr.IMG_H;
	gSystemMgr.imgCap->iImgWidth = gSystemMgr.IMG_W;
	gSystemLog.DisplayLogAndSave(cstrLog.GetBuffer(), LV_EVENT);
	hrender = XIS_CreateVideoRenderer(&guid_monitor, NULL, CAPTURE_COLORSPACE, gSystemMgr.IMG_W,
		gSystemMgr.IMG_H, TRUE, TRUE);
	if (hrender == NULL) {
		gSystemLog.DisplayLogAndSave(L"create video render fail.", LV_ERROR);
		return FALSE;
	}

	video_device_select = id;

	if (!resize_window(TRUE)) {
		gSystemLog.DisplayLogAndSave(L"resize window fail.", LV_ERROR);
		return FALSE;
	}
	return TRUE;
}

int TSW::init()
{
	XIS_Initialize();
	int dev_cnt = XIS_GetVideoCaptureCount();
	if (dev_cnt <= 0) {
		return FALSE;
	}

	dev_id = 0;
	cstrLog.Format(L"306e init down.id=%d", dev_id);
	gSystemLog.DisplayLogAndSave(cstrLog.GetBuffer(), LV_EVENT);
	return TRUE;
}

int TSW::run()
{
	return open_device_306e(dev_id);
}

int TSW::cap(/*BYTE *buf, long buf_size*/)
{

	return 1;

	//	log("T306E capture.");
	//memcpy(gSystemMgr.imgCap->bImgBuf, gSystemMgr.imgCap->bImgBuf, gSystemMgr.imgCap->iImgWidth*gSystemMgr.imgCap->iImgHeight * 3);
	//if (gSystemMgr.m_bMirrorX)
	//{
	//	if (gSystemMgr.m_bMirrorY)
	//	{
	//		for (int y = 0; y < gSystemMgr.imgCap->iBmpH; y++)
	//		{
	//			for (int x = 0; x < gSystemMgr.imgCap->iBmpW; x++)
	//			{
	//				gSystemMgr.imgCap->bBmpTestBuf[y * 3 * gSystemMgr.imgCap->iBmpW + x * 3 + 0] = gSystemMgr.imgCap->bBmpBuf[y * 3 * gSystemMgr.imgCap->iBmpW + x * 3];
	//				gSystemMgr.imgCap->bBmpTestBuf[y * 3 * gSystemMgr.imgCap->iBmpW + x * 3 + 1] = gSystemMgr.imgCap->bBmpBuf[y * 3 * gSystemMgr.imgCap->iBmpW + x * 3 + 1];
	//				gSystemMgr.imgCap->bBmpTestBuf[y * 3 * gSystemMgr.imgCap->iBmpW + x * 3 + 2] = gSystemMgr.imgCap->bBmpBuf[y * 3 * gSystemMgr.imgCap->iBmpW + x * 3 + 2];
	//			}
	//		}
	//	}
	//	else
	//	{
	//		for (int y = 0; y < gSystemMgr.imgCap->iBmpH; y++)
	//		{
	//			for (int x = 0; x < gSystemMgr.imgCap->iBmpW; x++)
	//			{
	//				gSystemMgr.imgCap->bBmpTestBuf[y * 3 * gSystemMgr.imgCap->iBmpW + x * 3 + 0] = gSystemMgr.imgCap->bBmpBuf[(gSystemMgr.imgCap->iBmpH - 1 - y) * 3 * gSystemMgr.imgCap->iBmpW + x * 3];
	//				gSystemMgr.imgCap->bBmpTestBuf[y * 3 * gSystemMgr.imgCap->iBmpW + x * 3 + 1] = gSystemMgr.imgCap->bBmpBuf[(gSystemMgr.imgCap->iBmpH - 1 - y) * 3 * gSystemMgr.imgCap->iBmpW + x * 3 + 1];
	//				gSystemMgr.imgCap->bBmpTestBuf[y * 3 * gSystemMgr.imgCap->iBmpW + x * 3 + 2] = gSystemMgr.imgCap->bBmpBuf[(gSystemMgr.imgCap->iBmpH - 1 - y) * 3 * gSystemMgr.imgCap->iBmpW + x * 3 + 2];
	//			}
	//		}
	//	}
	//}
	//else
	//{
	//	if (gSystemMgr.m_bMirrorY)
	//	{
	//		for (int y = 0; y < gSystemMgr.imgCap->iBmpH; y++)
	//		{
	//			for (int x = 0; x < gSystemMgr.imgCap->iBmpW; x++)
	//			{
	//				gSystemMgr.imgCap->bBmpTestBuf[y * 3 * gSystemMgr.imgCap->iBmpW + x * 3 + 0] = gSystemMgr.imgCap->bBmpBuf[y * 3 * gSystemMgr.imgCap->iBmpW + (gSystemMgr.imgCap->iBmpW - 1 - x) * 3];
	//				gSystemMgr.imgCap->bBmpTestBuf[y * 3 * gSystemMgr.imgCap->iBmpW + x * 3 + 1] = gSystemMgr.imgCap->bBmpBuf[y * 3 * gSystemMgr.imgCap->iBmpW + (gSystemMgr.imgCap->iBmpW - 1 - x) * 3 + 1];
	//				gSystemMgr.imgCap->bBmpTestBuf[y * 3 * gSystemMgr.imgCap->iBmpW + x * 3 + 2] = gSystemMgr.imgCap->bBmpBuf[y * 3 * gSystemMgr.imgCap->iBmpW + (gSystemMgr.imgCap->iBmpW - 1 - x) * 3 + 2];
	//			}
	//		}
	//	}
	//	else
	//	{
	//		for (int y = 0; y < gSystemMgr.imgCap->iBmpH; y++)
	//		{
	//			for (int x = 0; x < gSystemMgr.imgCap->iBmpW; x++)
	//			{
	//				gSystemMgr.imgCap->bBmpTestBuf[y * 3 * gSystemMgr.imgCap->iBmpW + x * 3 + 0] = gSystemMgr.imgCap->bBmpBuf[(gSystemMgr.imgCap->iBmpH - 1 - y) * 3 * gSystemMgr.imgCap->iBmpW + (gSystemMgr.imgCap->iBmpW - 1 - x) * 3];
	//				gSystemMgr.imgCap->bBmpTestBuf[y * 3 * gSystemMgr.imgCap->iBmpW + x * 3 + 1] = gSystemMgr.imgCap->bBmpBuf[(gSystemMgr.imgCap->iBmpH - 1 - y) * 3 * gSystemMgr.imgCap->iBmpW + (gSystemMgr.imgCap->iBmpW - 1 - x) * 3 + 1];
	//				gSystemMgr.imgCap->bBmpTestBuf[y * 3 * gSystemMgr.imgCap->iBmpW + x * 3 + 2] = gSystemMgr.imgCap->bBmpBuf[(gSystemMgr.imgCap->iBmpH - 1 - y) * 3 * gSystemMgr.imgCap->iBmpW + (gSystemMgr.imgCap->iBmpW - 1 - x) * 3 + 2];
	//			}
	//		}
	//	}
	//}
	return TRUE;
}

int TSW::stop()
{
	if (hproperty) {
		XIP_ClosePropertyHandle(hproperty);
		hproperty = NULL;
	}

	if (hvideo_device) {
		XIS_StopVideoCapture(hvideo_device);
		XIS_CloseVideoCapture(hvideo_device);
		hvideo_device = NULL;
	}

	if (hrender) {
		XIS_DestroyVideoRenderer(hrender);
		hrender = NULL;
	}
	video_device_select = -1;

	return TRUE;
}

int TSW::destroy()
{
	XIS_Uninitialize();

	return TRUE;
}
